package apputil

import (
	"encoding/binary"
)

func ReadText(data []byte) (text string, len int) {
	var res []byte
	for i, d := range data {
		if d == 0 {
			len = i
			len++
			break
		}
		res = append(res, d)
	}
	text = string(res)
	return text, len
}

func Int32ToByte(val int) []byte {
	d := make([]byte, 4)
	binary.BigEndian.PutUint32(d, uint32(val))
	return d
}

func Int16ToByte(val int) []byte {
	d := make([]byte, 2)
	binary.BigEndian.PutUint16(d, uint16(val))
	return d
}

func BytesToInt(val []byte) int32 {
	return int32(int(binary.BigEndian.Uint32(val)))
}

func BytesTouInt16(val []byte) uint16 {
	return binary.BigEndian.Uint16(val)
}
